const jwt = require('jsonwebtoken')
const { tokenSecret } = require('../../kum.config')
const User = require('./../models/userSchema')

async function checkToken(token){
  const decoded = await verify(token);
  if(decoded){
    const res = await User.findOne({
      token
    })
    if(res){
      return true
    }else{
      return false
    }
  }
  return false
}

function verify(token){
  return new Promise((resolve,reject)=>{
    jwt.verify(token, tokenSecret, function(err, decoded) {
      if(err){
        resolve(false)
      }else{
        resolve(decoded)
      }
    });
  })
}

module.exports = {checkToken}




